package com.mf.springboot.backend.userAuth.service.menu;

import com.mf.springboot.backend.mybatisModule.bean.TSysMenuDTO;
import com.mf.springboot.backend.mybatisModule.entity.menu.TSysMenu;
import com.mf.springboot.backend.mybatisModule.entity.menu.TSysMenuExample;
import com.mf.springboot.backend.mybatisModule.entity.menu.TSysRoleToMenu;
import com.mf.springboot.backend.mybatisModule.entity.menu.TSysRoleToMenuExample;
import com.mf.springboot.backend.mybatisModule.entity.roles.TSysUserToRole;
import com.mf.springboot.backend.mybatisModule.entity.roles.TSysUserToRoleExample;
import com.mf.springboot.backend.mybatisModule.mapper.TSysUserToRoleMapper;
import com.mf.springboot.backend.mybatisModule.mapper.menu.TSysMenuMapper;
import com.mf.springboot.backend.mybatisModule.mapper.menu.TSysRoleToMenuMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by java_jony on 2017/9/25.
 */
@Service
public class MenuService {
    @Resource
    private TSysMenuMapper menuMapper;
    @Resource
    private TSysUserToRoleMapper sysUserToRoleMapper;
    @Resource
    private TSysRoleToMenuMapper sysRoleToMenuMapper;

    public int deleteByPrimaryKey(String id) {
        return menuMapper.deleteByPrimaryKey(id);
    }

    public int insert(TSysMenu record) {
        return menuMapper.insert(record);
    }

    public List<TSysMenu> selectByExample(TSysMenuExample example) {
        return menuMapper.selectByExample(example);
    }

    public TSysMenu selectByPrimaryKey(String id) {
        return menuMapper.selectByPrimaryKey(id);
    }

    public int updateByPrimaryKey(TSysMenu record) {
        return menuMapper.updateByPrimaryKey(record);
    }


    public List<TSysMenuDTO> getMenus(String userId) {
        List<String> rolesId = getRolesId(userId);//先查角色ID
        List<String> menusId = getMenus(rolesId);//再查角色关联的菜单
        TSysMenuExample example = new TSysMenuExample();
        example.createCriteria().andIdIn(menusId);
        List<TSysMenu> menusList = menuMapper.selectByExample(example);
        return TSysMenuConvertTSysMenuDTO(menusList);
    }

    /*
     * 通过userId查询这个人关联的角色
     */
    public List<String> getRolesId(String userId) {
        TSysUserToRoleExample example = new TSysUserToRoleExample();
        example.createCriteria().andUserIdEqualTo(userId);
        List<TSysUserToRole> sysUserToRoles = sysUserToRoleMapper.selectByExample(example);
        List<String> roleIds = new ArrayList<String>();
        for (int i = 0; i < sysUserToRoles.size(); i++) {
            roleIds.add(sysUserToRoles.get(i).getRoleId());
        }
        return roleIds;
    }

    /*
     * 通过权限ID查找对应的菜单
     */
    public List<String> getMenus(List<String> rolesId) {
        TSysRoleToMenuExample example = new TSysRoleToMenuExample();
        example.createCriteria().andRoleIdIn(rolesId);
        List<TSysRoleToMenu> menus = sysRoleToMenuMapper.selectByExample(example);
        List<String> menusIds = new ArrayList<String>();
        for (int i = 0; i < menus.size(); i++) {
            menusIds.add(menus.get(i).getMenuId());
        }
        return menusIds;
    }


    public List<TSysMenuDTO> TSysMenuConvertTSysMenuDTO(List<TSysMenu> menusList) {
        List<TSysMenuDTO> menuDTOList = new ArrayList<TSysMenuDTO>();
        for (int i = 0; i < menusList.size(); i++) {
            TSysMenuDTO menuDTO = new TSysMenuDTO();
            System.out.println("=================" + menusList.get(i).getMenuName());
            menuDTO.setId(menusList.get(i).getId());
            menuDTO.setPath(menusList.get(i).getMenuPath());
            menuDTO.setTitle(menusList.get(i).getMenuName());
            TSysMenuExample example = new TSysMenuExample();
            example.createCriteria().andParentEqualTo(menusList.get(i).getId());
            List<TSysMenu> childrenList = menuMapper.selectByExample(example);
            if (childrenList.size() > 0) {
                menuDTO.setChildren(TSysMenuConvertTSysMenuDTO(childrenList));
            }
            menuDTOList.add(menuDTO);
        }
        return menuDTOList;
    }
}
